﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frontend.Grids;
using SpeakupBackend;
using SpeakupBackend.Themes;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Frontend
{
    class ClubThemeGrid: HighlightableGrid
    {
        public ClubThemeGrid()
        {
            HighlightBrush = Brushes.LightPink;

            Color color = Colors.LightGreen;
            CurrentBrush = new SolidColorBrush(Color.FromArgb(100, color.R, color.G, color.B));
        }

        BackendContext m_backendCtx;
        public BackendContext BackendCtx
        {
            get
            {
                return m_backendCtx;
            }
            set
            {
                if (m_backendCtx != value)
                {
                    m_backendCtx = value;                    
                }
            }
        }

        List<ClubLevelGroup> m_levelGroups;
        List<ClubLevelGroup> LevelGroups
        {
            get
            {
                if (m_levelGroups == null)
                {
                    m_levelGroups = ClubLevelGroup.ClubFactory.GetLevelGroups().ToList();
                }
                return m_levelGroups;
            }
        }        

        int IndexOf(ClubLevelGroup level)
        {
            return LevelGroups.IndexOf(level) + 1;
        }

        UIElementFactory UIElementFactory = new UIElementFactory();        

        public void SetupGrid()
        {
            Children.Clear();

            SetupRowHeaders();
            SetupColumnHeaders();
            SetupData();

            CurrentColumn = IndexOf(ClubLevelGroup.ClubFactory.GetLevelGroup(BackendCtx.Stats.Level));
        }

        void SetupRowHeaders()
        {
            RowDefinitions.Clear();

            Dictionary<ClubLevelGroup, int> levelCounters = new Dictionary<ClubLevelGroup,int>();
            foreach(ClubLevelGroup lg in LevelGroups)
            {
                levelCounters.Add(lg, 0);
            }

            foreach (ClubTheme theme in BackendCtx.ClubThemes)
            {
                levelCounters[theme.LevelGroup] = levelCounters[theme.LevelGroup] + 1;
            }

            int max = levelCounters.Values.Max();
            if (max < 5) max = 5;

            for (int i = 0; i < max + 1; i++)
            {
                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = new GridLength(1, GridUnitType.Star);
                RowDefinitions.Add(rowdef);
            }
        }

        void SetupColumnHeaders()
        {
            ColumnDefinitions.Clear();

            ColumnDefinition rowHeadersCol= new ColumnDefinition();
            rowHeadersCol.Width = new GridLength(0);
            ColumnDefinitions.Add(rowHeadersCol);

            for (int i = 0; i < LevelGroups.Count; i++)
            {
                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = new GridLength(1, GridUnitType.Star);
                ColumnDefinitions.Add(coldef);
            }

            foreach (ClubLevelGroup levelGroup in LevelGroups)
            {
                UIElement element = UIElementFactory.CreateLevelGroup(levelGroup);
                Grid.SetColumn(element, IndexOf(levelGroup));
                Grid.SetRow(element, 0);
                Children.Add(element);
            }
        }

        void SetupData()
        {
            Dictionary<ClubLevelGroup, int> levelCounters = new Dictionary<ClubLevelGroup, int>();
            foreach (ClubLevelGroup lg in LevelGroups)
            {
                levelCounters.Add(lg, 1);
            }

            foreach (ClubTheme theme in BackendCtx.ClubThemes)
            {
                UIElement element = UIElementFactory.CreateClubTheme(theme);

                Grid.SetRow(element, levelCounters[theme.LevelGroup]);                
                Grid.SetColumn(element, IndexOf(theme.LevelGroup));
                Children.Add(element);

                levelCounters[theme.LevelGroup] = levelCounters[theme.LevelGroup] + 1;
            }
        }

        protected override void OnHighlightChanged()
        {
        }
        
    }

}
